<?php

namespace app\api\model;

use app\common\model\BaseModel;
use think\facade\Db;
use think\Model;

class Car extends BaseModel
{
    // 表名
    protected $name = 'car';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];

    /**
     * 车辆列表
     */
    public function carList($requestData, $user){
        $page = $requestData['page']??1;
        $limit = $requestData['limit']??10;
        $status = $requestData['status']??0;
        $examine_status = $requestData['examine_status']??0;
        $cate_id = $requestData['cate_id']??0;
        $car_status = $requestData['car_status']??0;
        $car_where = ['is_delete' => 1, 'status' => 1];
        if($cate_id){
            $car_where['brand_id'] = $cate_id;
        }
        $car_ids = MerchantCar::where(['is_delete' => 1, 'merchant_id' => $user['id'], 'examine_status' => 2])->column('car_id');
        $car_id_list = self::where($car_where)->column('id');
        $car_id_list2 = self::where(['is_delete' => 1, 'status' => 1])->column('id');
        $final_car_ids = array_intersect($car_ids, $car_id_list2);
        $brand_id_list = \app\api\model\Car::where(['id' => $final_car_ids])->column('brand_id');
        $carCateInfo = CarCate::field('id,cate_name')->where(['type' => 1, 'id' => $brand_id_list])->select()->toArray();

        $where = [['merchant_id', '=', $user['id']], ['is_delete', '=', 1]];
        if($status){
            $where[] = ['status', '=', $status];
            $where[] = ['examine_status', '=', 2];
        }
        if($examine_status){
            $where[] = ['examine_status', '=', $examine_status];
        }
        if($car_status){
            $where[] = ['car_status', '=', $car_status];
            $where[] = ['status', '=', 1];
            $where[] = ['examine_status', '=', 2];
        }
        $where[] = ['car_id', 'in', implode(',', $car_id_list)];
        $carList = MerchantCar::field('id,car_id,merchant_car_name,car_price,car_number,registration_time,car_label,createtime,car_status,examine_status,status,reason,examine_time,drive,displacement')
            ->with('carss')
            ->where($where)
            ->page($page,$limit)
            ->select()
            ->toArray();
        foreach ($carList as $key=>$val){
            $carList[$key]['createtime'] = date('Y-m-d H:i:s', $val['createtime']);
            $carList[$key]['examine_time'] = $val['examine_time'] ? date('Y-m-d H:i:s', $val['examine_time']) : 0;
            $carList[$key]['registration_time'] = $val['registration_time'] ? date('Y-m-d', $val['registration_time']) : '';
            $car_gear = $val['car_gear'] == 1 ? '手动' : '自动';
            $carList[$key]['car_sign'] = $val['car_site'] . '座|' . $val['car_gate'] . '门|' . $car_gear . '|'. $val['displacement'];
            $labels = '';
            if($val['car_label']){
                $labels = Label::where('id', 'in', $val['car_label'])->column('name');
                $labels = implode(',', $labels);
            }
            $carList[$key]['car_label'] = $labels;
            //临时下架时间  只展示上月之后的
            $temporary_time = MerchantCarTime::field('id,start_time,end_time')->where(['is_delete' => 1, 'merchant_car_id' => $val['id']])->order('createtime', 'desc')->select()->toArray();
            foreach ($temporary_time as $kk=>$vv){
                $temporary_time[$kk]['start_time'] = date('Y-m-d H:i', $vv['start_time']);
                $temporary_time[$kk]['end_time'] = date('Y-m-d H:i', $vv['end_time']);
            }
            $carList[$key]['temporary_time'] = $temporary_time;
            $carList[$key]['car_price'] = floatval($val['car_price']);
        }
        //已上架和已下架 审核中和已拒绝车辆数量
        $car_id_list = self::where(['is_delete' => 1, 'status' => 1])->column('id');
        $on_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 2, 'status' => 1])->where('car_id', 'in', $car_id_list)->count();
        $off_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 2, 'status' => 2])->where('car_id', 'in', $car_id_list)->count();
        $review_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 1])->where('car_id', 'in', $car_id_list)->count();
        $fail_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 3])->where('car_id', 'in', $car_id_list)->count();
        //已租车辆和可租车辆
        $use_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 2, 'status' => 1, 'car_status' => 2])->where('car_id', 'in', $car_id_list)->count();
        $unuse_number = MerchantCar::where(['merchant_id' => $user['id'], 'is_delete' => 1, 'examine_status' => 2, 'status' => 1, 'car_status' => 1])->where('car_id', 'in', $car_id_list)->count();
        $newData = [
            'cateData' => $carCateInfo,
            'on_number' => $on_number,
            'off_number' => $off_number,
            'review_number' => $review_number,
            'fail_number' => $fail_number,
            'use_number' => $use_number,
            'unuse_number' => $unuse_number,
            'carData' => $carList,
        ];
        return ['status' => 1, 'message' => '请求成功', 'result' => $newData];
    }

    /**
     * 平台车辆列表
     */
    public function platformCar($requestData, $user){
        $page = $requestData['page']??1;
        $limit = $requestData['limit']??10;
        $brand_id = $requestData['brand_id']??0;
        $model_id = $requestData['model_id']??0;
        $type_id = $requestData['type_id']??0;
        $where = ['is_delete' => 1, 'status' => 1];
        if($brand_id){
            $where['brand_id'] = $brand_id;
        }
        if($model_id){
            $where['model_id'] = $model_id;
        }
        if($type_id){
            $where['type_id'] = $type_id;
        }
        $list = self::field('id,brand_id,model_id,car_name,car_image,car_site,car_gate,car_gear,drive,displacement')->where($where)->order('sort desc,createtime desc,brand_id desc,model_id desc')->page($page, $limit)->select()->toArray();
        foreach ($list as $key => $value){
            $car_gear = $value['car_gear'] == 1 ? '手动' : '自动';
            $label = $value['car_site'].'座|'.$value['car_gate'].'门|'.$car_gear.'|'.$value['displacement'];
            $list[$key]['label'] = $label;
        }
        return ['status' => 1, 'message' => '请求成功', 'result' => $list];
    }

    /**
     * 添加车辆
     */
    public function saveCar($requestData, $user){
        $last_one = MerchantCar::where(['merchant_id' => $user['id']])->order('createtime', 'desc')->value('createtime');
        if(time() - $last_one < 10){
            return ['status' => 0, 'message' => '提交频繁', 'result' => ''];
        }
        $validate = new \app\api\validate\MerchantCar();
        if (!$validate->scene('add')->check($requestData)) {
            return ['status' => 0, 'message' => $validate->getError(), 'result' => ''];
        }
        $requestData['merchant_id'] = $user['id'];
        $requestData['status'] = 1;
        $requestData['examine_status'] = 1;
        $requestData['merchant_longitude'] = $user['longitude'];
        $requestData['merchant_longitude'] = $user['longitude'];
        $requestData['registration_time'] = strtotime($requestData['registration_time']);
        $car = \app\api\model\Car::where(['is_delete' => 1, 'id' => $requestData['car_id']])->find();
        if(!$car){
            return ['status' => 0, 'message' => '平台车辆不存在', 'result' => ''];
        }
        $requestData['brand_id'] = $car['brand_id'];
        $requestData['model_id'] = $car['model_id'];
        $requestData['type_id'] = $car['type_id'];
        //判断车牌号唯一
        $car_number = $requestData['car_number']??'';
        if($car_number){
            $car_find = MerchantCar::where(['is_delete' => 1, 'car_number' => $car_number])->count();
            if($car_find){
                return ['status' => 0, 'message' => '车牌号已存在', 'result' => ''];
            }
        }
        Db::startTrans();
        try {
            $model = new MerchantCar();
            $result = $model->allowField([])->save($requestData);
            if($result){
                $id = $model->id;
                //添加临时下架时间
                if(isset($requestData['temporary_time']) && $requestData['temporary_time']){
                    $insert_time = [];
                    foreach ($requestData['temporary_time'] as $k=>$v){
                        if(!$v['start_time'] || !$v['end_time']){
                            Db::rollback();
                            return ['status' => 0, 'message' => '某一临时下架时间为空值', 'result' => ''];
                        }
                        $insert_time[] = [
                            'merchant_car_id' => $id,
                            'start_time' => strtotime($v['start_time']),
                            'end_time' => strtotime($v['end_time']),
                            'createtime' => time()
                        ];
                    }
                    if($insert_time){
                        MerchantCarTime::insertAll($insert_time);
                    }
                }

                //添加远期价格
                if(isset($requestData['far_price']) && $requestData['far_price']){
                    $insert_price = [];
                    foreach ($requestData['far_price'] as $kk=>$vv){
                        if(!$vv['start_time'] || !$vv['end_time'] || !$vv['price']){
                            Db::rollback();
                            return ['status' => 0, 'message' => '某一远期价格的时间或价格为空值', 'result' => ''];
                        }
                        $insert_price[] = [
                            'merchant_car_id' => $id,
                            'start_time' => strtotime($vv['start_time']),
                            'end_time' => strtotime($vv['end_time'] . ' 23:59:59'),
                            'days' => $vv['days'],
                            'price' => $vv['price'],
                            'createtime' => time()
                        ];
                    }
                    if($insert_price){
                        MerchantCarPrice::insertAll($insert_price);
                    }
                }
                //添加保险
                if(isset($requestData['car_insurance']) && $requestData['car_insurance']){
                    $insure_list  = Insure::where(['is_delete' => 1])->column('insure_name', 'id');
                    $insert_insurance = [];
                    foreach ($requestData['car_insurance'] as $kk=>$vv){
                        if($vv['price'] === ''){
                            Db::rollback();
                            return ['status' => 0, 'message' => '某一保险价格为空值', 'result' => ''];
                        }
                        $insert_insurance[] = [
                            'merchant_car_id' => $id,
                            'insure_id' => $vv['insure_id'],
                            'insure_name' => $insure_list[$vv['insure_id']]??'',
                            'price' => $vv['price'],
                            'createtime' => time()
                        ];
                    }
                    if($insert_insurance){
                        MerchantCarInsure::insertAll($insert_insurance);
                    }
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
        }
        if ($result){
            return ['status' => 1, 'message' => '添加成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '添加失败', 'result' => ''];
        }

    }

    /**
     * 编辑车辆
     */
    public function editCar($requestData, $user){
        $type = $requestData['type']??0;
        $id = $requestData['id']??0;
        if(!$type || !in_array($type, [1,2,3]) || !$id){
            return ['status' => 0, 'message' => '请求错误', 'result' => ''];
        }
        $model = new MerchantCar();
        $merchant_car = $model->field('id,car_id,merchant_car_name,banner,car_price,car_deposit,car_note,status,examine_status,createtime,car_number,registration_time,driving_license,check_image,car_label,handle_fee,night_pick_fee,night_return_fee,car_status,insure_file,examine_status')->where(['is_delete' => 1, 'id' => $id])->find();
        if(!$merchant_car){
            return ['status' => 0, 'message' => '商家车辆不存在', 'result' => ''];
        }
        if($merchant_car['car_status'] == 2){
            return ['status' => 0, 'message' => '车辆出租中，无法提交编辑审核', 'result' => ''];
        }
        if($type == 1){//查看
            $car = \app\api\model\Car::where(['is_delete' => 1, 'id' => $merchant_car['car_id']])->find();
            $merchant_car['car_price'] = floatval($merchant_car['car_price']);
            $merchant_car['car_deposit'] = floatval($merchant_car['car_deposit']);
            $merchant_car['handle_fee'] = floatval($merchant_car['handle_fee']);
            $merchant_car['night_pick_fee'] = floatval($merchant_car['night_pick_fee']);
            $merchant_car['night_return_fee'] = floatval($merchant_car['night_return_fee']);
            $merchant_car['brand_id'] = $car['brand_id']??0;
            $merchant_car['model_id'] = $car['model_id']??0;
            $merchant_car['type_id'] = $car['type_id']??0;
            $merchant_car['banner'] = explode(',', $merchant_car['banner']);
            $merchant_car['registration_time'] = date('Y-m-d', $merchant_car['registration_time']);
            $merchant_car['check_image'] = explode(',', $merchant_car['check_image']);
            $merchant_car['car_label'] = explode(',', $merchant_car['car_label']);
            $merchant_car['insure_file'] = $merchant_car['insure_file'] ? explode(',', $merchant_car['insure_file']) : [];
            //临时下架时间
            $temporary_time = MerchantCarTime::field('id,start_time,end_time')->where(['is_delete' => 1, 'merchant_car_id' => $id])->order('createtime', 'desc')->select()->toArray();
            foreach ($temporary_time as $ke=>$va){
                $temporary_time[$ke]['start_time'] = date('Y-m-d H:i', $va['start_time']);
                $temporary_time[$ke]['end_time'] = date('Y-m-d H:i', $va['end_time']);
            }
            $merchant_car['temporary_time'] = $temporary_time;
            //远期价格
            $far_price = MerchantCarPrice::field('id,start_time,end_time,days,price')->where(['is_delete' => 1, 'merchant_car_id' => $id])->order('createtime', 'desc')->select()->toArray();
            foreach ($far_price as $ky=>$vl){
                $far_price[$ky]['start_time'] = date('Y-m-d', $vl['start_time']);
                $far_price[$ky]['end_time'] = date('Y-m-d', $vl['end_time']);
                $far_price[$ky]['price'] = floatval($vl['price']);
            }
            $merchant_car['far_price'] = $far_price;
            //保险
            $insure = Insure::field('id as insure_id,insure_name')->where(['is_delete' => 1, 'status' => 1])->select()->toArray();
            $merchant_car_insurance = MerchantCarInsure::where(['is_delete' => 1, 'merchant_car_id' => $id])->order('createtime', 'desc')->column('price', 'insure_id');
            foreach ($insure as $key=>$value){
                $insure[$key]['price'] = 0;
                if(isset($merchant_car_insurance[$value['insure_id']])){
                    $insure[$key]['price'] = floatval($merchant_car_insurance[$value['insure_id']]);
                }
            }
            $merchant_car['car_insurance'] = $insure;
            return ['status' => 1, 'message' => '请求成功', 'result' => $merchant_car];
        }else{//提交
            $validate = new \app\api\validate\MerchantCar();
            $scene = $type == 2 ? 'edit' : 'price';
            if (!$validate->scene($scene)->check($requestData)) {
                return ['status' => 0, 'message' => $validate->getError(), 'result' => ''];
            }
            $requestData['merchant_longitude'] = $user['longitude'];
            $requestData['merchant_longitude'] = $user['longitude'];
            $requestData['updatetime'] = time();
            if(isset($requestData['registration_time']) && $requestData['registration_time']){
                $requestData['registration_time'] = strtotime($requestData['registration_time']);
            }

            if(isset($requestData['car_id'])){
                $car = \app\api\model\Car::where(['is_delete' => 1, 'id' => $requestData['car_id']])->find();
                if(!$car){
                    return ['status' => 0, 'message' => '平台车辆不存在', 'result' => ''];
                }
                $requestData['brand_id'] = $car['brand_id'];
                $requestData['model_id'] = $car['model_id'];
                $requestData['type_id'] = $car['type_id'];
                if($merchant_car['examine_status'] == 3){
                    $requestData['examine_status'] = 1;
                }
            }

            if($merchant_car['examine_status'] == 2 && $type == 2){
                $requestData['examine_status'] = 1;
            }
            //判断车牌号唯一
            $car_number = $requestData['car_number']??'';
            if($car_number){
                $car_find = MerchantCar::where(['is_delete' => 1, 'car_number' => $car_number])->where('id', '<>', $id)->count();
                if($car_find){
                    return ['status' => 0, 'message' => '车牌号已存在', 'result' => ''];
                }
            }
            Db::startTrans();
            try {
                $result = $merchant_car->allowField([])->save($requestData);
                if($result){
                    //添加临时下架时间
                    if(isset($requestData['temporary_time'])){
                        $old_id_list = MerchantCarTime::where(['is_delete' => 1, 'merchant_car_id' => $id])->column('id');
                        $last_id_list = [];
                        $insert_time = [];
                        $update_time = [];
                        if($requestData['temporary_time']){
                            foreach ($requestData['temporary_time'] as $k=>$v){
                                if(!$v['start_time'] || !$v['end_time']){
                                    Db::rollback();
                                    return ['status' => 0, 'message' => '某一临时下架时间为空值', 'result' => ''];
                                }
                                if(isset($v['id']) && $v['id']){
                                    $update_time[] = [
                                        'id' => $v['id'],
                                        'start_time' => strtotime($v['start_time']),
                                        'end_time' => strtotime($v['end_time']),
                                        'updatetime' => time()
                                    ];
                                    $last_id_list[] = intval($v['id']);
                                }else{
                                    $insert_time[] = [
                                        'merchant_car_id' => $id,
                                        'start_time' => strtotime($v['start_time']),
                                        'end_time' => strtotime($v['end_time']),
                                        'createtime' => time()
                                    ];
                                }
                            }
                        }
                        $del_id_list = array_diff($old_id_list, $last_id_list);
                        if($del_id_list){
                            MerchantCarTime::where(['is_delete' => 1, 'merchant_car_id' => $id])->whereIn('id', implode(',', $del_id_list))->update(['is_delete' => 2, 'deletetime' => time()]);
                        }
                        if($insert_time){
                            MerchantCarTime::insertAll($insert_time);
                        }
                        if($update_time){
                            $time_model = new MerchantCarTime();
                            $time_model->saveAll($update_time);
                        }
                    }
                    //添加远期价格
                    if(isset($requestData['far_price'])){
                        $old_id_list = MerchantCarPrice::where(['is_delete' => 1, 'merchant_car_id' => $id])->column('id');
                        $last_id_list = [];
                        $insert_price = [];
                        $update_price = [];
                        if($requestData['far_price']){
                            foreach ($requestData['far_price'] as $kk=>$vv){
                                if(!$vv['start_time'] || !$vv['end_time']){
                                    Db::rollback();
                                    return ['status' => 0, 'message' => '某一远期价格的时间为空值', 'result' => ''];
                                }
                                if(isset($vv['id']) && $vv['id']){
                                    $update_price[] = [
                                        'id' => $vv['id'],
                                        'start_time' => strtotime($vv['start_time']),
                                        'end_time' => strtotime($vv['end_time'] . ' 23:59:59'),
                                        'days' => $vv['days'],
                                        'price' => $vv['price'],
                                        'createtime' => time()
                                    ];
                                    $last_id_list[] = intval($vv['id']);
                                }else{
                                    $insert_price[] = [
                                        'merchant_car_id' => $id,
                                        'start_time' => strtotime($vv['start_time']),
                                        'end_time' => strtotime($vv['end_time'] . ' 23:59:59'),
                                        'days' => $vv['days'],
                                        'price' => $vv['price'],
                                        'createtime' => time()
                                    ];
                                }
                            }
                        }
                        $del_id_list = array_diff($old_id_list, $last_id_list);
                        if($del_id_list){
                            MerchantCarPrice::where(['is_delete' => 1, 'merchant_car_id' => $id])->whereIn('id', implode(',', $del_id_list))->update(['is_delete' => 2, 'deletetime' => time()]);
                        }
                        if($insert_price){
                            MerchantCarPrice::insertAll($insert_price);
                        }
                        if($update_price){
                            $price_model = new MerchantCarPrice();
                            $price_model->saveAll($update_price);
                        }
                    }
                    //添加保险
                    if(isset($requestData['car_insurance'])){
                        $insert_insurance = [];
                        $update_insurance = [];
                        $insure_list  = Insure::where(['is_delete' => 1])->column('insure_name', 'id');
                        foreach ($requestData['car_insurance'] as $k=>$v){
                            $old_insurance = MerchantCarInsure::where(['is_delete' => 1, 'merchant_car_id' => $id, 'insure_id' => $v['insure_id']])->find();
                            if($old_insurance){
                                $update_insurance[] = [
                                    'id' => $old_insurance['id'],
                                    'price' => $v['price'],
                                    'insure_name' => $insure_list[$old_insurance['insure_id']]??'',
                                    'updatetime' => time()
                                ];
                            }else{
                                $insert_insurance[] = [
                                    'merchant_car_id' => $id,
                                    'insure_id' => $v['insure_id'],
                                    'insure_name' => $insure_list[$v['insure_id']]??'',
                                    'price' => $v['price'],
                                    'createtime' => time()
                                ];
                            }
                        }

                        if($insert_insurance){
                            MerchantCarInsure::insertAll($insert_insurance);
                        }
                        if($update_insurance){
                            $time_model = new MerchantCarInsure();
                            $time_model->saveAll($update_insurance);
                        }
                    }
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
            }
            if ($result){
                return ['status' => 1, 'message' => '添加成功', 'result' => ''];
            }else{
                return ['status' => 0, 'message' => '添加失败', 'result' => ''];
            }
        }


    }

    /**
     * 临时下架时间添加
     */
    public function temporaryTimeAdd($requestData, $user){
        $merchant_car_id = $requestData['merchant_car_id']??0;
        $start_time = $requestData['start_time']??'';
        $end_time = $requestData['end_time']??'';
        if(!$merchant_car_id || !$start_time || !$end_time){
            return ['status' => 0, 'message' => '请求错误', 'result' => ''];
        }
        $merchant_car = MerchantCar::field('id,car_price,car_deposit,handle_fee,night_pick_fee,night_return_fee')->where(['is_delete' => 1, 'id' => $merchant_car_id])->find();
        if(!$merchant_car){
            return ['status' => 0, 'message' => '商家车辆不存在', 'result' => ''];
        }
        $requestData['start_time'] = strtotime($start_time);
        $requestData['end_time'] = strtotime($end_time);
        $requestData['createtime'] = time();
        Db::startTrans();
        try {
            $model = new MerchantCarTime();
            $result = $model->allowField([])->save($requestData);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
        }
        if ($result){
            return ['status' => 1, 'message' => '添加成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '添加失败', 'result' => ''];
        }
    }

    /**
     * 临时下架时间删除
     */
    public function temporaryTimeDel($requestData, $user){
        $id = $requestData['id']??0;
        if(!$id){
            return ['status' => 0, 'message' => '请求错误', 'result' => ''];
        }
        $time = MerchantCarTime::where(['is_delete' => 1, 'id' => $id])->find();
        if(!$time){
            return ['status' => 0, 'message' => '临时下架时间不存在', 'result' => ''];
        }
        Db::startTrans();
        try {
            $result = MerchantCarTime::where('id', $id)->update(['is_delete' => 2, 'deletetime' => time()]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
        }
        if ($result){
            return ['status' => 1, 'message' => '删除成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '删除失败', 'result' => ''];
        }
    }

    /**
     * 车辆上下架
     */
    public function editStatus($requestData, $user){
        $id = $requestData['id']??0;
        $status = $requestData['status']??0;
        if(!$id || !$status || !in_array($status, [1,2])){
            return ['status' => 0, 'message' => '请求错误', 'result' => ''];
        }
        $car = MerchantCar::where(['is_delete' => 1, 'id' => $id])->find();
        if(!$car){
            return ['status' => 0, 'message' => '临时下架时间不存在', 'result' => ''];
        }
        if($status == 1 && $car['status'] == 1){
            return ['status' => 0, 'message' => '车辆已经上架', 'result' => ''];
        }
        if($status == 2 && $car['status'] == 2){
            return ['status' => 0, 'message' => '车辆已经下过架', 'result' => ''];
        }
        Db::startTrans();
        try {
            $result = MerchantCar::where('id', $id)->update(['status' => $status, 'updatetime' => time()]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
        }
        if ($result){
            return ['status' => 1, 'message' => '请求成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '请求失败', 'result' => ''];
        }
    }

    /**
     * 车辆删除
     */
    public function delCar($requestData, $user){
        $id = $requestData['id']??0;
        if(!$id){
            return ['status' => 0, 'message' => '请求错误', 'result' => ''];
        }
        $car = MerchantCar::where(['is_delete' => 1, 'id' => $id])->find();
        if(!$car){
            return ['status' => 0, 'message' => '车辆不存在', 'result' => ''];
        }
        Db::startTrans();
        try {
            $result = MerchantCar::where('id', $id)->update(['is_delete' => 2, 'deletetime' => time()]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 0, 'message' => $e->getMessage(), 'result' => ''];
        }
        if ($result){
            return ['status' => 1, 'message' => '删除成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '删除失败', 'result' => ''];
        }
    }

    /**
     * 价格日历
     */
    public function priceCalendar($requestData, $user){
        $merchant_car_id = $requestData['merchant_car_id']??0;
        $date = $requestData['date']??'';
        if(!$merchant_car_id || !$date){
            return ['status' => 0, 'message' => '参数错误', 'result' => ''];
        }

        $merchant_car = MerchantCar::field('id,car_price')->where(['is_delete' => 1, 'id' => $merchant_car_id])->find();
        if(!$merchant_car){
            return ['status' => 0, 'message' => '车辆不存在', 'result' => ''];
        }
        $merchant_car['price'] = floatval($merchant_car['price']);
        $time_date = strtotime($date);
        $time_last = strtotime(date('Y-m-t', $time_date) . '23:59:59');
        $far_price_list = \app\admin\model\MerchantCarPrice::field('id,start_time,end_time,price')->where(['is_delete' => 1, 'merchant_car_id' => $merchant_car_id])->where("start_time > $time_date or end_time > $time_date")->select()->toArray();
        $new_data = [];
        foreach ($far_price_list as &$value){
            $all_date = getDatesBetween($value['start_time'], $value['end_time']);
            foreach ($all_date as $kk=>$vv){
                if(strtotime($vv) >= $time_date && strtotime($vv) <= $time_last){
                    $new_data[] = [
                        'date' => $vv,
                        'price' => floatval($value['price'])
                    ];
                }
            }
        }
        unset($value);
        $result_data = [
            'merchant_car' => $merchant_car,
            'far_price_list' => $new_data
        ];

        return ['status' => 1, 'message' => '请求成功', 'result' => $result_data];
    }

    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return Driver[]|array|\think\Collection
     */
    public static function getList($where = [], $field = '*', $order = 'createtime desc', $page = 1, $limit = 10, $with = ''){
        if($with){
            return self::withMin($with, 'car_price')->where($where)->field($field)->order('id', 'desc')->order($order)->page($page, $limit)->select();
        }else{
            return self::where($where)->field($field)->order('id', 'desc')->order($order)->page($page, $limit)->select();
        }
    }

    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return Driver[]|array|\think\Collection
     */
    public static function getCarList($where = [], $field = '*', $order = 'createtime desc'){
        return self::where($where)->field($field)->order('id', 'desc')->order($order)->select();
    }

    /**
     * 获取单条
     * @param array $where
     * @param string $field
     * @return Driver|array|mixed|\think\Model|null
     */
    public static function getFind($where = [], $field = '*'){
        return self::where($where)->field($field)->find();
    }

    public function merchantCarOne(){
        return $this->hasOne('MerchantCar', 'car_id')->bind(['car_price'])->joinType('inner');
    }

    public function merchantCar(){
        return $this->hasMany('MerchantCar', 'car_id');
    }

    public function merchantCars(){
        return $this->hasMany('MerchantCar', 'car_id');
    }

}
